const express = require('express');
const router = express.Router();
const fs = require('fs');
const path = require('path');

router.post('/set-params', async (req, res) => {
    try {
        const { dataset_name, rm_fn } = req.body;

        // 读取train.py文件
        const trainPath = path.join(__dirname, '../../../../FlowTM-main/train.py');
        let content = fs.readFileSync(trainPath, 'utf8');

        // 更新参数
        content = content.replace(/--dataset', type=str, default="[^"]*"/, `--dataset', type=str, default="${dataset_name}"`);
        content = content.replace(/--data-root', type=str, default="[^"]*"/, `--data-root', type=str, default="${path.dirname(rm_fn)}"`);

        // 写回文件
        fs.writeFileSync(trainPath, content);

        res.json({ success: true, message: 'Parameters updated successfully' });
    } catch (error) {
        console.error('Error updating parameters:', error);
        res.status(500).json({ success: false, message: 'Failed to update parameters' });
    }
});

module.exports = router; 